package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.entity.DeviceData;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        //1.设置分页条件
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        //传递的是时间戳要转换成时间类型
        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(deviceDataPageReqDto.getStartTime()) ? null : LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime()));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(deviceDataPageReqDto.getEndTime()) ? null : LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime()));
        //2.到数据库查询设备数据
        Page<DeviceDataVo> deviceDataPage = deviceDataMapper.getDeviceDataPage(
                deviceDataPageReqDto.getDeviceName(),
                deviceDataPageReqDto.getFunctionId(),
                deviceDataPageReqDto.getStartDate(),
                deviceDataPageReqDto.getEndDate());
        //3.返回
        return PageResponse.of(deviceDataPage,DeviceDataVo.class);
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataGraph(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos = deviceDataMapper.queryDeviceDataGraph(iotId,functionId,startDate,endDate);
        Map<String, Double> map = deviceDataGraphVos.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        List<DeviceDataGraphVo> graphVoList = DeviceDataGraphVo.dayInstance(startDate);
        graphVoList.forEach(graphVo -> {
            Double dataValue = map.get(graphVo.getDateTime());
            if (ObjectUtil.isNotEmpty(dataValue)) {
                graphVo.setDataValue(dataValue);
            }
        });
        return graphVoList;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos = deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId,startDate,endDate);
        Map<String, Double> map = deviceDataGraphVos.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        List<DeviceDataGraphVo> graphVoList = DeviceDataGraphVo.dayInstance(startDate);
        graphVoList.forEach(graphVo -> {
            Double dataValue = map.get(graphVo.getDateTime());
            if (ObjectUtil.isNotEmpty(dataValue)) {
                graphVo.setDataValue(dataValue);
            }
        });
        return graphVoList;
    }
}
